package org.jctc.els.modules.biz.service.customer.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jctc.els.common.constant.MyConstant;
import org.jctc.els.modules.biz.entity.user.User;
import org.jctc.els.modules.biz.mapper.customer.CustomerMapper;
import org.jctc.els.modules.biz.mapper.user.UserMapper;
import org.jctc.els.modules.biz.service.customer.ICustomerService;
import org.jctc.els.modules.biz.entity.customer.Customer;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客服表 服务实现类
 * </p>
 *
 * @author lyh
 * @since 2021-03-29
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean save(Customer customer) {
		Long userId = AuthUtil.getUserId();
		User userExpand = userMapper.selectById(userId);
        customer.setCreateUserName(userExpand.getUserName());
		customer.setIsEnable(MyConstant.STATUS_ENABLED);
		customer.setCreateTime(new Date());
        return customerMapper.insert(customer) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return customerMapper.deleteById(id) > 0;
    }

    @Override
    public boolean edit(Customer customer) {
		Long userId = AuthUtil.getUserId();
		User userExpand = userMapper.selectById(userId);
        customer.setCreateUserName(userExpand.getUserName());
        return customerMapper.updateById(customer) > 0;
    }

    @Override
    public Page<Customer> selectListByPage(Integer pageNum, Integer pageSize) {
        Page<Customer> customerPage = new Page<>(pageNum, pageSize);
        Customer customer = new Customer();
        customer.setIsEnable(1);
        return (Page<Customer>) customerMapper.selectPage(customerPage, Wrappers.query(customer));
    }

    @Override
    public Customer selectById(Long id) {
        return customerMapper.selectById(id);
    }
}
